﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Space_Cleaner.SpaceCleaner
{
    class SC_Keyboard
    {
        private LinkedList<SC_IKeyboardListener> listeners;

        private KeyboardState oldState;

        public SC_Keyboard()
        {
            this.listeners = new LinkedList<SC_IKeyboardListener>();
        }

        public void addListener(SC_IKeyboardListener _listener)
        {
            this.listeners.AddLast(_listener);
        }

        public void update(TimeSpan elapsedTime)
        {
            KeyboardState ks = Keyboard.GetState();
            if (oldState != null)
            {
                if (ks.IsKeyDown(Keys.Right))
                    notifyKRotRight();

                if (ks.IsKeyDown(Keys.Left))
                    notifyKRotLeft();

                if (ks.IsKeyDown(Keys.Up))
                    notifyKRotAccelerate();

                if (ks.IsKeyDown(Keys.Down))
                    notifyKBreak();

                if (ks.IsKeyDown(Keys.Space))
                    notifyKMagnet();
            }

            oldState = ks;
        }

        private void notifyKRotRight()
        {
            foreach (SC_IKeyboardListener listener in this.listeners)
                listener.onKRotRight();
        }

        private void notifyKRotLeft()
        {
            foreach (SC_IKeyboardListener listener in this.listeners)
                listener.onKRotLeft();
        }

        private void notifyKRotAccelerate()
        {
            foreach (SC_IKeyboardListener listener in this.listeners)
                listener.onKAccelerate();
        }

        private void notifyKBreak()
        {
            foreach (SC_IKeyboardListener listener in this.listeners)
                listener.onKBreak();
        }

        private void notifyKMagnet()
        {
            foreach (SC_IKeyboardListener listener in this.listeners)
                listener.onKMagnet();
        }
    }
}
